// pages/user/user.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statistics: {},
    pageList: [],
    showCopyLink: false,
    link: '',
    config: config,
    setting: {},
    baseUrl: config.basePath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "博主"
    });
    this.getSetting();
    this.getStatistics();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShareTimeline(res) {
    return {
      title: config.miniName + '-博主',
      path: '/pages/user/user'
    }
  },

  getStatistics: function () {
    let that = this;
    wx.request({
      url: config.basePath + '/mini/system/statistics',
      method: 'GET',
      success(res) {
        if (res.data.code === 200) {
          that.setData({
            statistics: res.data.data,
          });
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: res.data.msg
          })
        }
      },
      fail(err) {
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },

  pageList: function () {
    let that = this;
    that.setData({
      pageList: [{
          "title": "友情链接",
          "icon": "user",
          "link": "/pages/link/link",
          "mini": true
        },
        {
          "title": "我的网站",
          "icon": "address",
          "link": config.basePath,
          "mini": false
        }
      ],
    });

    if (that.data.setting.aboutId != "") {
      that.setData({
        pageList: that.data.pageList.concat([{
          "title": "关于博主",
          "icon": "warning",
          "link": "/pages/article/article?id=" + that.data.setting.aboutId,
          "mini": true
        }])
      });
    }
    wx.request({
      url: config.basePath + '/mini/system/pluginIsStart',
      method: 'GET',
      data: {
        pluginId: "photos"
      },
      success(res) {
        if (res.data.code === 200) {
          if (res.data.data) {
            that.setData({
              pageList: [{
                "title": "相册",
                "icon": "picture",
                "link": "/pages/photos/photos",
                "mini": true
              }].concat(that.data.pageList)
            });
          }
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: '服务出错'
          })
        }
      },
      fail(err) {
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },

  goPage: function (event) {
    let that = this;
    if (event.currentTarget.dataset.mini) {
      wx.navigateTo({
        url: event.currentTarget.dataset.link
      })
    } else {
      that.setData({
        link: event.currentTarget.dataset.link,
        showCopyLink: true
      });
    }
  },
  copyLink: function () {
    let that = this;
    wx.setClipboardData({
      data: that.data.link,
      success: function (res) {}
    })
  },
  getSetting: function () {
    let that = this;
    wx.request({
      url: config.basePath + '/mini/system/getSetting',
      method: 'GET',
      success(res) {
        if (res.data.code === 200) {
          that.setData({
            setting: res.data.data
          });
        }
        that.pageList();
      },
      fail(err) {
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  }
})